

# 自定义过滤器  1> 参数接收模板变量  2> 将转换结果返回



def func_index_convert(index):  # index = 4

    index_dict = {1: "first", 2: "second", 3: "third"}

    return index_dict.get(index, "")



import functools
from info.models import User
from flask import session, g
def user_login_data(f):

    @functools.wraps(f)

    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        user = None # type:User
        if user_id:
            from flask import current_app
            try:
                user = User.query.get(user_id)
            except BaseException as e:
                current_app.logger.error(e)

        g.user = user   # 把查询出来的对象，给上下文变量 g，这样其它文件也可以用

        return f(*args, **kwargs)

    return wrapper

def file_upload(data):
    import qiniu
    access_key = 'XGvKFOlKvftdyTF5Qv7A7sNACDhAXJWLtDY1XCe3'
    secret_key = '4UZclhVnKiaKwCu9buP-NEJBa468IoCupEu7i2g_'
    bucket_name = 'infonews'

    q = qiniu.Auth(access_key,secret_key)
    key = None
    token = q.upload_token(bucket_name)
    ret, info = qiniu.put_data(token,key,data)

    if ret is not None:
        return ret.get('key')
    else:
        raise BaseException(info)
